package com.wash.shoes.controller.pc;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.wash.shoes.mapper.ShopMapper;
import com.wash.shoes.mapper.TimelinessWarnMapper;
import com.wash.shoes.mapper.UserShopMapper;
import com.wash.shoes.request.StatisticsRequest;
import com.wash.shoes.resp.StatisticsResp;
import com.wash.shoes.resp.UserBindVo;
import com.wash.shoes.util.HeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("PC统计")
@RequiredArgsConstructor
@RequestMapping("/pc/statistics")
public class StatisticsController {
    //时效统计
    private final TimelinessWarnMapper timelinessWarnMapper;

    @ApiOperation("单量监控-获取48小时妥投量")
    @GetMapping("/fortyEighthDeliveryQuantity")
    public R fortyEighthDeliveryQuantity(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.fortyEighthDeliveryQuantity(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @ApiOperation("单量监控-在厂量,订单状态不是取消、不是送鞋中、不是取鞋中")
    @GetMapping("/inFactoryQuantity")
    public R inFactoryQuantity(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.inFactoryQuantity(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @ApiOperation(value = "单量监控-入厂待预检量", notes = "订单状态是待入库")
    @GetMapping("/inFactoryWaitUnpacking")
    public R inFactoryWaitUnpacking(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.inFactoryWaitUnpacking(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @ApiOperation(value = "单量监控-在厂待质检量", notes = "订单状态是清洗完成")
    @GetMapping("/waitingForQualityInspection")
    public R waitingForQualityInspection(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.waitingForQualityInspection(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @ApiOperation(value = "单量监控-质检待出厂量", notes = "订单状态是待出库")
    @GetMapping("/qualityWaitOutFactory")
    public R qualityWaitOutFactory(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.qualityWaitOutFactory(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }


    @ApiOperation(value = "今日产能指标-今日预检衣服量", notes = "今日预检衣服")
    @GetMapping("/todayPreCheckClothes")
    public R todayPreCheckClothes(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.todayPreCheckClothes(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @GetMapping("/todayQualityClothes")
    @ApiOperation(value = "今日产能指标-今日质检衣服量", notes = "今天质检完成的订单量")
    public R todayQualityClothes(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.todayQualityClothes(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @GetMapping("/todayOutput")
    @ApiOperation(value = "今日产能指标-今日出厂量", notes = "今日出库的定单量")
    public R todayOutput(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.todayOutput(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @GetMapping("/over24HUnpacking")
    @ApiOperation(value = "超时告警-订单状态是待入库超过24H的", notes = "订单状态是待入库超过24H的")
    public R over24HUnpacking(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.over24HUnpacking(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @GetMapping("/over72HUnpacking")
    @ApiOperation(value = "超时告警-订单状态是待入库超过72H的", notes = "订单状态是待入库超过72H的")
    public R over72HUnpacking(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.over72HUnpacking(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @GetMapping("/inFactoryOver4Days")
    @ApiOperation(value = "超时告警-在厂超过4天", notes = "订单状态不是取消、送鞋中、取鞋中 时间是大于4天的")
    public R inFactoryOver4Days(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.inFactoryOver4Days(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @ApiOperation(value = "超时告警-在厂超过6天", notes = "订单状态不是取消、送鞋中、取鞋中 时间是大于6天的")
    @GetMapping("/inFactoryOver6Days")
    public R inFactoryOver6Days(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.inFactoryOver6Days(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @GetMapping("/todayWorkOrder")
    @ApiOperation(value = "工单指标-今日工单", notes = "今日工单")
    public R todayWorkOrder(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.todayWorkOrder(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @ApiOperation(value = "工单指标-12H未处理工单", notes = "12H未处理工单")
    @GetMapping("/twelveHoursUnprocessedWorkOrder")
    public R twelveHoursUnprocessedWorkOrder(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.twelveHoursUnprocessedWorkOrder(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @GetMapping("/twentyFourHoursUnprocessedWorkOrder")
    @ApiOperation(value = "工单指标-24H未处理工单", notes = "24H未处理工单")
    public R twentyFourHoursUnprocessedWorkOrder(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.twentyFourHoursUnprocessedWorkOrder(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @ApiOperation(value = "工单指标-48H未处理工单", notes = "48H未处理工单")
    @GetMapping("/fortyEightHoursUnprocessedWorkOrder")
    public R fortyEightHoursUnprocessedWorkOrder(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.fortyEightHoursUnprocessedWorkOrder(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @ApiOperation(value = "工单指标-近三天工单量", notes = "近三天工单量")
    @GetMapping("/threeDaysWorkOrder")
    public R threeDaysWorkOrder(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.threeDaysWorkOrder(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    @GetMapping("/reverseWashOrder")
    @ApiOperation(value = "返洗指标-反洗订单量", notes = "反洗订单量")
    public R reverseWashOrder(StatisticsRequest statisticsRequest) {
        UserBindVo userBindVo = getUserBindType();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        List<String> barndIdList = getBrandIdList(userId);
        return R.ok(timelinessWarnMapper.reverseWashOrder(statisticsRequest.getFactoryId(), userBindVo.getShopIdStr(), userBindVo.getUserBindType(), barndIdList, statisticsRequest.getBrandId()));
    }

    public UserBindVo getUserBindType() {
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        // 获取用户绑定的类型,
        List<String> userBindTypeList = userShopMapper.getUserBindType(userId + "");
        String userBindType = null;
        if (!CollectionUtils.isEmpty(userBindTypeList)) {
            userBindType = userBindTypeList.get(0);
        }
        String shopIdStr = HeaderUtil.getShopIdStr();
        return UserBindVo.builder().userBindType(userBindType).shopIdStr(shopIdStr).build();
    }


    @Autowired
    private UserShopMapper userShopMapper;
    @Autowired
    private ShopMapper shopMapper;

    @PostMapping("/all")
    @ApiOperation(value = "统计", notes = "统计")
    public R<StatisticsResp> statistics(@RequestBody StatisticsRequest statisticsRequest) {
        StatisticsResp statisticsResp = new StatisticsResp();
        // 用户账号绑定的类型
        Long userId = SecurityUtils.getUserId();
        // 获取用户绑定的类型,
        List<String> userBindTypeList = userShopMapper.getUserBindType(userId + "");
        String userBindType = null;
        if (!CollectionUtils.isEmpty(userBindTypeList)) {
            userBindType = userBindTypeList.get(0);
        }
        List<String> barndIdList = getBrandIdList(userId);

        String shopIdStr = HeaderUtil.getShopIdStr();
        statisticsResp.setFortyEighthDeliveryQuantity(timelinessWarnMapper.fortyEighthDeliveryQuantity(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//单量监控-获取48小时妥投量
        statisticsResp.setInFactoryQuantity(timelinessWarnMapper.inFactoryQuantity(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//单量监控-在厂量,订单状态不是取消、不是送鞋中、不是取鞋中
        statisticsResp.setInFactoryWaitUnpacking(timelinessWarnMapper.inFactoryWaitUnpacking(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//单量监控-入厂待预检量
        statisticsResp.setWaitingForQualityInspection(timelinessWarnMapper.waitingForQualityInspection(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//单量监控-在厂待质检量
        statisticsResp.setQualityWaitOutFactory(timelinessWarnMapper.qualityWaitOutFactory(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//单量监控-质检待出厂量
        statisticsResp.setTodayPreCheckClothes(timelinessWarnMapper.todayPreCheckClothes(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//今日产能指标-今日预检衣服量
        statisticsResp.setTodayQualityClothes(timelinessWarnMapper.todayQualityClothes(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//今日产能指标-今日质检衣服量
        statisticsResp.setTodayOutput(timelinessWarnMapper.todayOutput(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//今日产能指标-今日出厂量
        statisticsResp.setOver24HUnpacking(timelinessWarnMapper.over24HUnpacking(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//超时告警-订单状态是待入库超过24H的
        statisticsResp.setOver72HUnpacking(timelinessWarnMapper.over72HUnpacking(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//超时告警-订单状态是待入库超过72H的

        statisticsResp.setInFactoryOver4Days(timelinessWarnMapper.inFactoryOver4Days(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//超时告警-在厂超过4天
        statisticsResp.setInFactoryOver6Days(timelinessWarnMapper.inFactoryOver6Days(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//超时告警-在厂超过6天

        statisticsResp.setTodayWorkOrder(timelinessWarnMapper.todayWorkOrder(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//工单指标-今日工单
        statisticsResp.setTwelveHoursUnprocessedWorkOrder(timelinessWarnMapper.twelveHoursUnprocessedWorkOrder(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//工单指标-12H未处理工单
        statisticsResp.setTwentyFourHoursUnprocessedWorkOrder(timelinessWarnMapper.twentyFourHoursUnprocessedWorkOrder(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//工单指标-24H未处理工单
        statisticsResp.setFortyEightHoursUnprocessedWorkOrder(timelinessWarnMapper.fortyEightHoursUnprocessedWorkOrder(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//工单指标-48H未处理工单
        statisticsResp.setThreeDaysWorkOrder(timelinessWarnMapper.threeDaysWorkOrder(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//工单指标-近三天工单量
        statisticsResp.setReverseWashOrder(timelinessWarnMapper.reverseWashOrder(statisticsRequest.getFactoryId(), shopIdStr, userBindType, barndIdList, statisticsRequest.getBrandId()));//工单指标-反洗订单量
        return R.ok(statisticsResp);
    }

    private List<String> getBrandIdList(Long userId) {
        List<String> barndIdList = null;
        //是否有品牌管理员角色
        if (SecurityUtils.hasRole("brandmanager")) {
            //取当前账号关联了哪些店铺,在通过店铺id去查询品牌id,然后把品牌id传入进统计sql
            barndIdList = shopMapper.getBrandIdByUserId(userId);
        }
        return barndIdList;
    }
}